﻿using Entity = Vinno.WMS.Pack.Core.Entities.MaterialInStorage;
using IContractService = Vinno.WMS.Pack.Core.IWmsService;
using InputDto = Vinno.WMS.Pack.Core.Dtos.MaterialInStorageInputDto;
using OutputDto = Vinno.WMS.Pack.Core.Dtos.MaterialInStorageOutputDto;

namespace Vinno.WMS.Apis.Areas.Admin.Controllers
{
    [Description("入库信息")]
    [ModuleInfo(OrderNo = 6, Position = "Wms", PositionName = "仓储模块")]
    public class MaterialInStorageController : AdminControllerBase
    {
        /// <summary>
        /// 获取 服务接口
        /// </summary>
        protected IContractService ContractService => ServiceProvider.GetService<IContractService>();

        /// <summary>
        /// 读取
        /// </summary>
        [HttpPost]
        [Description("读取")]
        [ModuleInfo]
        public PageData<OutputDto> Read(PageRequest request)
        {
            Check.NotNull(request, nameof(request));
            IFunction function = this.GetExecuteFunction();

            Expression<Func<Entity, bool>> predicate = FilterService.GetExpression<Entity>(request.FilterGroup);
            var page = ContractService.MaterialInStorages.ToPage(predicate,
                request.PageCondition, m => new
                {
                    D = m,
                    IPN = m.Material.IPN,
                    WareHouse = m.Material.WareHouse,
                    WareHouseArea = m.Material.WareHouseArea,
                    WareHouseAreaPos = m.Material.WareHouseAreaPos
                }).ToPageResult(data => data.Select(m =>
                {
                    var outDto = m.D.MapTo<OutputDto>();
                    outDto.IPNId = m.IPN.Id;
                    outDto.IPNNo = m.IPN.IpnNo;
                    outDto.IPNName = m.IPN.Name;
                    outDto.IPNDescription = m.IPN.Description;
                    outDto.IPNUnit = m.IPN.Unit;
                    outDto.WareHouseAreaPosId = m.WareHouseAreaPos.Id;
                    outDto.WareHouseName = m.WareHouse.Name;
                    outDto.WareHouseAreaName = m.WareHouseArea.AreaName;
                    outDto.WareHouseAreaPosName = m.WareHouseAreaPos.AreaPosName;

                    return outDto;
                }).ToArray());

            return page.ToPageData();
        }

        /// <summary>
        /// 新增
        /// </summary>
        [HttpPost]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("新增")]
        [ModuleInfo]
        public async Task<AjaxResult> Create(InputDto dto)
        {
            Check.NotNull(dto, nameof(dto));
            var result = await ContractService.CreateMaterialInStorage(dto);
            return result.ToAjaxResult();
        }

        /// <summary>
        /// 更新
        /// </summary>
        [HttpPost]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("更新")]
        [ModuleInfo]
        public async Task<AjaxResult> Update(InputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            List<string> ids = new();
            List<OutputDto> outputDtos = new();
            foreach (var dto in dtos)
            {
                var result = await ContractService.UpdateMaterialInStorages(dto);
                if (!result.Succeeded)
                {
                    return result.ToAjaxResult();
                }
                ids.Add(dto.Id.ToString());
                outputDtos.Add(dto.MapTo<OutputDto>());
            }
            return AjaxResult.Success(outputDtos, $"{nameof(Entity)}“{ids.ExpandAndToString()}”更新成功");
        }

        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("删除")]
        [ModuleInfo]
        public async Task<AjaxResult> Delete(Guid id)
        {
            Check.NotEmpty(id, nameof(id));
            var result = await ContractService.DeleteMaterialInStorage(id);
            return result.ToAjaxResult();
        }

    }
}